package com.wswy.wzcx.ui.car

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.support.v4.content.ContextCompat
import com.che.libcommon.api.ApiOptionalResultObserver
import com.che.libcommon.api.BaseResult
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.api.Gov122
import com.wswy.wzcx.model.CarTypes
import com.wswy.wzcx.model.OCRResult
import com.wswy.wzcx.module.LocManager
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.UserViewModel
import timber.log.Timber
import java.io.File
import java.util.*

open class AddCarPreViewModel : UserViewModel() {

    class CarNoBgConfig(val bgResDrawable:Int,val textColor:Int)
    private companion object{

        val carNoColors = hashMapOf<String,CarNoBgConfig>().apply {

            put(CarTypes.CAR_SMALL, CarNoBgConfig(R.drawable.car_no_small_bg,ContextCompat.getColor(AppContext.getContext(),R.color.car_no_small)))
            put(CarTypes.CAR_BIG,CarNoBgConfig(R.drawable.car_no_big_bg,ContextCompat.getColor(AppContext.getContext(),R.color.car_no_big)))
            put(CarTypes.MOTO,CarNoBgConfig(R.drawable.car_no_moto_bg,ContextCompat.getColor(AppContext.getContext(),R.color.car_no_moto)))
            put(CarTypes.NE_CAR_BIG,CarNoBgConfig(R.drawable.car_no_ne_bg,ContextCompat.getColor(AppContext.getContext(),R.color.car_no_ne)))
            put(CarTypes.NE_CAR_SMALL,CarNoBgConfig(R.drawable.car_no_ne_bg,ContextCompat.getColor(AppContext.getContext(),R.color.car_no_ne)))

        }

    }


    private val defaultCarProvLiveData = MutableLiveData<String?>()

    protected val ocrResult = MutableLiveData<Resource<OCRResult>>()

    override fun onCreate() {
        super.onCreate()
        loadDefCarNo()
    }
    
    private fun loadDefCarNo(){
        LocManager.getInstance().currentWZCity?.areaCode?.run {
            //取前2位
            if (length >= 2){

                defaultCarProvLiveData.postValue(Gov122.findProvNameByCode(substring(0,2)))

            }

        }
    }

    fun scannerOCR(path:String?){

        addRun(Api.get().ocrDrivingLicence(File(path)).subscribeWith(object :ApiOptionalResultObserver<OCRResult>(){
            override fun onStart() {
                super.onStart()
                ocrResult.postValue(Resource.loading(null))
            }

            override fun onApiResult(t: OCRResult?) {
                ocrResult.postValue(Resource.success(t))
            }

            override fun onApiError(result: BaseResult<*>?) {
                ocrResult.postValue(Resource.error(result))
            }

        }))

//        addRun(Api.get().OCRDriverLicense2(path).subscribeWith(object :ApiOptionalResultObserver<OCRResult>(){
//            override fun onStart() {
//                super.onStart()
//                ocrResult.postValue(Resource.loading(null))
//            }
//
//            override fun onApiResult(t: OCRResult?) {
//                ocrResult.postValue(Resource.success(t))
//            }
//
//            override fun onApiError(result: BaseResult<*>?) {
//                ocrResult.postValue(Resource.error(result))
//            }
//
//        }))


    }

    fun getOcrResultLiveData():LiveData<Resource<OCRResult>> = ocrResult

    fun getDefaultCarNoProvLiveData():LiveData<String?> = defaultCarProvLiveData


    fun getCarNoColorConfig(carType:String?):CarNoBgConfig{
        return carNoColors.get(carType)?: carNoColors.get(CarTypes.CAR_SMALL)!!
    }

}